<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;
class GoodsController extends Controller
{
    public function show($id)
    {
        $config=DB::table('config')->get();
        $list=DB::table('type')->get();
        $count=DB::table('comment')->where('gid',$id)->count();
        $comment=DB::table('comment')->where('gid',$id)->get();
        $reply=DB::table('reply')->join('comment','reply.cid','=','comment.id')->where('comment.gid',$id)->get();
        $goods=DB::table('goods')->where('id',$id)->first();
        return view('home.goods',['list'=>$list,'config'=>$config,'goods'=>$goods,'comment'=>$comment,'count'=>$count,'reply'=>$reply]);
    }
    public function store(Request $request)
    {
        $count=DB::table('order')->where([
            ['uid','=',session()->get('homeuser')->id],
            ['gid','=',$request['gid']],
            ['state','=','4'],
        ])->count();
        if ($count==0) {
            return back()->with('msg','您没有成功购买该商品,不能发表评论!');
        }
        $data=$request->except('_token');
        $data['uid']=session()->get('homeuser')->id;
        $id=DB::table('comment')->insertGetId($data);
        if ($id>0) {
            return back();
        }
    }
}
